#!/bin/sh
TIME_FILE=/tmp/time.synced

logtosys()
{
  dmesg | grep T: | (
    lasttime=0
    while IFS='[] ' read space level service version timestamp humandate humantime ntpsync junk; do
      if [ "$service" = "T:" ] && [ "$ntpsync" = "ntp=1" ]; then
        lasttime=$timestamp
      fi
    done
    if [ "$lasttime" -gt 0 ]; then
      toolbox date +%s -s $lasttime
    fi
    #TODO(irinams): create the TIME_FILE only when we have a way to approximate
    # how much time was spent during reboot to add it to to lasttime
    #echo >"TIME_FILE"
  )
}

case "$1" in
  start)
    echo "UTC" > /tmp/TZ

    if [ -c /dev/rtc0 ] ; then
      babysit 60 systohc-loop &
    else
      logtosys
    fi

    (
      wait-until-created /tmp/ntp.synced
      echo : >"$TIME_FILE"
    ) &
    ;;
  stop)
    if [ -c /dev/rtc0 ] ; then
      pkillwait -x systohc-loop
    fi
    rm -f "$TIME_FILE"
    ;;
  restart|reload)
    $0 stop; $0 start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac
